﻿Public Class MainForm
    Dim syncTimer As Timer
    Dim oldCaption As String = ""
    Dim IsSyncing As Boolean = False

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Me.Visible = False
            Me.ContextMenuStrip1.Enabled = False
            If My.Settings.FirstTime Then
                DeleteSyncedItems()
                'go through first time setup
                Dim frm As New FirstTimeForm
                If frm.ShowDialog = DialogResult.OK Then
                    'clear first time flag
                    My.Settings.FirstTime = False
                    My.Settings.Save()
                    'perform initial sync
                    If PerformInitialSyncIfNeeded() Then
                        'let's show the settings window
                        ShowSettingsForm()
                    End If
                Else
                    'user cancelled first time setup, so exit app
                    Me.Close()
                    Exit Sub
                End If
            Else
                'this is not the first time
                If PerformInitialSyncIfNeeded() Then
                    'if an initail sync was performed the show settings form
                    ShowSettingsForm()
                End If
            End If
            Me.NotifyIcon1.Visible = True
            Me.ContextMenuStrip1.Enabled = True

            'create timer
            syncTimer = New Timer()
            AddHandler syncTimer.Tick, AddressOf syncTimer_Tick
            With syncTimer
                .Interval = My.Settings.SyncEvery * 60 * 1000
                .Enabled = True
                .Start()
            End With
        Catch ex As Exception
            MessageBox.Show("Error starting up.  " + ex.Message)
        Finally
            Try
                Me.ContextMenuStrip1.Enabled = True
            Catch ex As Exception

            End Try
        End Try
    End Sub

    Sub syncTimer_Tick()
        If My.Settings.CanAutoSync Then
            StartSync()
        End If
    End Sub

    Function PerformInitialSyncIfNeeded() As Boolean
        Try
            If My.Settings.PerformedInitialSync = False Then
                Me.ContextMenuStrip1.Enabled = False
                Dim frm As New InitialSyncForm
                If frm.ShowDialog() = DialogResult.OK Then
                    My.Settings.PerformedInitialSync = True
                    My.Settings.Save()
                    Return True
                Else
                    Me.Close()
                    Return False
                End If
            End If
            Return False
        Finally
            Me.ContextMenuStrip1.Enabled = True
        End Try
    End Function

    Sub StartSync()
        If Not IsSyncing Then
            If My.Computer.Network.IsAvailable Then
                If My.Computer.Network.Ping("www.rememberthemilk.com") Then
                    IsSyncing = True
                    Dim sync As SyncManager = SyncManager.StartSync
                    oldCaption = NotifyIcon1.Text
                    'cmdSyncNow.Caption = "Syncing..."
                    'cmdSyncNow.Enabled = False
                    'cmdSettings.Enabled = False
                    NotifyIcon1.Text = "Syncing, please wait..."
                    ContextMenuStrip1.Enabled = False
                    'sync.Begin()
                    AddHandler sync.BeforeSync, AddressOf BeforeSync
                    AddHandler sync.AfterSync, AddressOf AfterSync
                End If
            End If
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        ShowSettingsForm()
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        ShowSettingsForm()
    End Sub

    Private Sub BeginSyncToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BeginSyncToolStripMenuItem.Click
        StartSync()
    End Sub

    Sub BeforeSync()

    End Sub

    Delegate Sub EmptyDelegate()

    Sub AfterSync()
        If Me.InvokeRequired Then
            Me.Invoke(New EmptyDelegate(AddressOf AfterSync))
        Else
            IsSyncing = False
            NotifyIcon1.Text = oldCaption
            ContextMenuStrip1.Enabled = True
        End If
    End Sub

    Private Sub ShowSettingsForm()
        If ContextMenuStrip1.Enabled Then
            ContextMenuStrip1.Enabled = False
            SettingsForm.ShowDialog()
            ContextMenuStrip1.Enabled = True
        End If
    End Sub


End Class